package com.tboss.template.sample.dwr.treeviewService;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.tboss.template.sample.dwr.treeviewDao.TreeviewDao;
import com.tboss.template.sample.dwr.treeviewDto.TreeviewDto;

public class TreeviewJsonManualService implements TreeviewService {

	@Override
	public StringBuffer build(HashMap<String, String> opts){
		StringBuffer rtnValue = new StringBuffer();
		
		String rootKey = opts.get("jsonRootKey");
		List<TreeviewDto> list = new LinkedList<TreeviewDto>();
		int idx = 0;
		for(int i = 0; i < 2; i++){
			TreeviewDto mainMenu = new TreeviewDto();
			mainMenu.setFnId(String.valueOf(idx++));
			mainMenu.setFnParentId("0");
			mainMenu.setFnAlias("Item " + (i+1));
			mainMenu.setFnServletUrl("");
			mainMenu.setFnDesc("");
			mainMenu.setFnOwner("");
			mainMenu.setFnGroup("");
			mainMenu.setFnUseYN("Y");
			mainMenu.setFnOrder(i);
			
			list.add(mainMenu);
			
			if(i == 1){
				for(int j = 0; j < 3; j++){
					String name = (j == 2)?" (closed at start)":"";
					Boolean collapse = (j == 2)?true:false;

					TreeviewDto sub1Menu = new TreeviewDto();
					sub1Menu.setFnId(String.valueOf(idx++));
					sub1Menu.setFnParentId(mainMenu.getFnId());
					sub1Menu.setFnAlias("Item " + (i+1) + "." + (j+1) + name);
					sub1Menu.setFnServletUrl("");
					sub1Menu.setFnDesc("");
					sub1Menu.setFnOwner("");
					sub1Menu.setFnGroup("");
					sub1Menu.setFnUseYN("Y");
					sub1Menu.setFnCollapse(collapse);
					sub1Menu.setFnOrder(j);
					
					list.add(sub1Menu);
					
					if(j == 0 || j == 2){
						for(int k = 0; k < 2; k++){
							String url = "";
							switch (j){
								case 0:
									switch (k){
										case 0: url = "/treeview/link1"; break;
										case 1: url = "/treeview/link2"; break;
										default: url = "";
									}
									break;
								case 2:
									switch (k){
										case 0: url = "/treeview/link"; break;
										default: url = "";
									}
									break;
								default : url = "";
							}
							
							TreeviewDto sub2Menu = new TreeviewDto();
							sub2Menu.setFnId(String.valueOf(idx++));
							sub2Menu.setFnParentId(sub1Menu.getFnId());
							sub2Menu.setFnAlias("Item " + (i+1) + "." + (j+1) + "." + (k+1));
							sub2Menu.setFnServletUrl(url);
							sub2Menu.setFnDesc("");
							sub2Menu.setFnOwner("");
							sub2Menu.setFnGroup("");
							sub2Menu.setFnUseYN("Y");
							sub2Menu.setFnOrder(k);
							
							list.add(sub2Menu);
						}
					}
				}
			}
		}
		
		try {
			Map<String, List<TreeviewDto>> menu = new HashMap<String, List<TreeviewDto>>();
			menu.put(rootKey, list);

			rtnValue.append(new ObjectMapper().writeValueAsString(menu));
			
		} catch (JsonGenerationException e) { e.printStackTrace();
		} catch (JsonMappingException e) { e.printStackTrace();
		} catch (IOException e) { e.printStackTrace();
		}
		
		return rtnValue;
	}

	@Override
	public StringBuffer build(HashMap<String, String> opts,
			TreeviewDao treeviewDao) {
		// TODO Auto-generated method stub
		return null;
	}

}
